﻿using Arction.Wpf.Charting;
using Arction.Wpf.Charting.Axes;
using Arction.Wpf.Charting.Views.ViewXY;
using HighThroughput.Common;
using HighThroughput.ShareData;
using OSSUtility;
using OSSUtility.Charts;
using Ozone.BLL;
using Ozone.Help;
using Ozone.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ozone.Views.Widget
{
    /// <summary>
    /// UCLineChart.xaml 的交互逻辑
    /// </summary>
    public partial class UCLineChart : UserControl
    {
        public ModuleLineChart moduleLineChart = null;
        List<string> lstDataIDs = new List<string>();

        string FDataID = "";
        string minY = "0", maxY = "20";
        int nDevIndex = 0;
        public UCLineChart()
        {
            InitializeComponent();
            moduleLineChart = new ModuleLineChart("", gridLinechart);
            moduleLineChart.setLegendBoxesShow(true);
        }

        S10SetupPanelParam setupParam = null;
        private void getParam()
        {
            setupParam = ChartParamBLL.get();
            string val = setupParam.getVal(FDataID);
            string[] arV = val.Split(',');
            if (arV.Length == 2)
            {
                minY = arV[0];
                maxY = arV[1];
            }

        }

        private void saveParam()
        {
            string val = txtTempMin.Text + "," + txtTempMax.Text;
            setupParam.setVal(FDataID, val);
            ChartParamBLL.save(setupParam);
        }


        public void setDevIndex(int nIndex, string dataID)
        {
            FDataID = dataID;
            nDevIndex = nIndex;

            getParam();

            if (maxY != "")
            {
                txtTempMin.Text = minY;
                txtTempMax.Text = maxY;

                setYRangPro(Utility.str2int(txtTempMin.Text), Utility.str2int(txtTempMax.Text));
            }
            Start();
        }

        public void Start()
        {
            RefreshLineThread();
        }

        public void Stop()
        {
            bRunFlag = false;
        }

        public void addLine(string plsTitle, string plsTag, int nLineColorIndex)
        {
            moduleLineChart.addLine(plsTitle, plsTag, nLineColorIndex);

            lstDataIDs.Add(plsTag);
        }

        private void TextBoxTRang_LostFocus(object sender, RoutedEventArgs e)
        {
            setYRangPro(Utility.str2int(txtTempMin.Text), Utility.str2int(txtTempMax.Text));

            saveParam();
        }

        public void setYRangPro(int min, int max)
        {
            moduleLineChart.setYRang(min, max);
        }

        DateTime DTStart;
        bool bRunFlag = false;
        private void RefreshLineThread()
        {
            DTStart = DateTime.Now;
            bRunFlag = true;

            Task.Run(() =>
            {
                while (bRunFlag)
                {
                    Thread.Sleep(1000);

                }
            });

        }

        private void refreshLineData()
        {

        }

        private void AddPoint(ModuleLineChart mlc, double v, string tag)









        {
            try
            {
                SeriesPoint[] aPoints = new SeriesPoint[1];
                aPoints[0].X = mlc.DateTimeToAxisValue(DateTime.Now);
                aPoints[0].Y = v;
                mlc.AddPoints(tag, aPoints);

                LightningChart cartesianChart = mlc.getCartesianChart();
                ViewXY chartView = cartesianChart.ViewXY;
                AxisX axisX = chartView.XAxes[0];
                axisX.SetRange(mlc.DateTimeToAxisValue(DTStart)
                         , mlc.DateTimeToAxisValue(DateTime.Now));

            }
            catch (Exception ex)
            {
           
            }
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
